#include "scaleDialog.h"

ScaleDialog::ScaleDialog(QList<QTreeWidgetItem *> _list, QWidget *_parent) : QDialog(_parent), list(_list){
    setWindowTitle(QString::fromUtf8("Escala"));
    scaleX = scaleY = scaleZ = 1;
    relativePositionX = relativePositionY = relativePositionZ = 0;
    useRelativePosition = false;

    QLabel *scaleXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *scaleXDoubleSpinBox = new QDoubleSpinBox();
    scaleXDoubleSpinBox->setRange(0.01, 50.0);
    scaleXDoubleSpinBox->setSingleStep(1.0);
    scaleXDoubleSpinBox->setValue(scaleX);
    connect(scaleXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeScaleX(double)));


    QLabel *scaleYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *scaleYDoubleSpinBox = new QDoubleSpinBox();
    scaleYDoubleSpinBox->setRange(0.01, 50.0);
    scaleYDoubleSpinBox->setSingleStep(1.0);
    scaleYDoubleSpinBox->setValue(scaleY);
    connect(scaleYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeScaleY(double)));


    QLabel *scaleZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *scaleZDoubleSpinBox = new QDoubleSpinBox();
    scaleZDoubleSpinBox->setRange(0.01, 50.0);
    scaleZDoubleSpinBox->setSingleStep(1.0);
    scaleZDoubleSpinBox->setValue(scaleZ);
    connect(scaleZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeScaleZ(double)));


    QHBoxLayout *scaleLayout = new QHBoxLayout;
    scaleLayout->addWidget(scaleXLabel);
    scaleLayout->addWidget(scaleXDoubleSpinBox);
    scaleLayout->addStretch(1);
    scaleLayout->addWidget(scaleYLabel);
    scaleLayout->addWidget(scaleYDoubleSpinBox);
    scaleLayout->addStretch(1);
    scaleLayout->addWidget(scaleZLabel);
    scaleLayout->addWidget(scaleZDoubleSpinBox);
    scaleLayout->addStretch(1);

    QGroupBox *scaleGroupBox = new QGroupBox(QString::fromUtf8("Escala"));
    scaleGroupBox->setLayout(scaleLayout);



    QLabel *relativePositionXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *relativePositionXDoubleSpinBox = new QDoubleSpinBox();
    relativePositionXDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionXDoubleSpinBox->setSingleStep(1.0);
    relativePositionXDoubleSpinBox->setValue(relativePositionX);
    connect(relativePositionXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionX(double)));


    QLabel *relativePositionYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *relativePositionYDoubleSpinBox = new QDoubleSpinBox();
    relativePositionYDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionYDoubleSpinBox->setSingleStep(1.0);
    relativePositionYDoubleSpinBox->setValue(relativePositionY);
    connect(relativePositionYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionY(double)));


    QLabel *relativePositionZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *relativePositionZDoubleSpinBox = new QDoubleSpinBox();
    relativePositionZDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionZDoubleSpinBox->setSingleStep(1.0);
    relativePositionZDoubleSpinBox->setValue(relativePositionZ);
    connect(relativePositionZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionZ(double)));


    QHBoxLayout *relativePositionLayout = new QHBoxLayout;
    relativePositionLayout->addWidget(relativePositionXLabel);
    relativePositionLayout->addWidget(relativePositionXDoubleSpinBox);
    relativePositionLayout->addStretch(1);
    relativePositionLayout->addWidget(relativePositionYLabel);
    relativePositionLayout->addWidget(relativePositionYDoubleSpinBox);
    relativePositionLayout->addStretch(1);
    relativePositionLayout->addWidget(relativePositionZLabel);
    relativePositionLayout->addWidget(relativePositionZDoubleSpinBox);
    relativePositionLayout->addStretch(1);


    QGroupBox *relativePositionGroupBox = new QGroupBox(QString::fromUtf8("Posição relativa"));
    relativePositionGroupBox->setLayout(relativePositionLayout);
    relativePositionGroupBox->setCheckable(true);
    relativePositionGroupBox->setChecked(useRelativePosition);
    connect(relativePositionGroupBox, SIGNAL(clicked(bool)), this, SLOT(changeUseRelativePosition(bool)));


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(scaleGroupBox);
    mainLayout->addWidget(relativePositionGroupBox);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);

}

ScaleDialog::~ScaleDialog(){
}

void ScaleDialog::acceptAndCreate(){
    if( (scaleX == 0) && (scaleY == 0) && (scaleZ == 0) ) accept();

    Vector3D scaleVector(scaleX, scaleY, scaleZ);
    Vector3D relativePosition(relativePositionX, relativePositionY, relativePositionZ);
    for(int i = 0; i < list.size(); i++){
        QTreeWidgetItem *item = list.at(i);
        ((ObjectTreeItem *)item)->getObject()->scale(scaleVector, relativePosition, useRelativePosition);
    }

    accept();
    done();
}

void ScaleDialog::changeScaleX(double _x){
    scaleX = _x;
}

void ScaleDialog::changeScaleY(double _y){
    scaleY = _y;
}

void ScaleDialog::changeScaleZ(double _z){
    scaleZ = _z;
}

void ScaleDialog::changeRelativePositionX(double _x){
    relativePositionX = _x;
}

void ScaleDialog::changeRelativePositionY(double _y){
    relativePositionY = _y;
}

void ScaleDialog::changeRelativePositionZ(double _z){
    relativePositionZ = _z;
}

void ScaleDialog::changeUseRelativePosition(bool _b){
    useRelativePosition = _b;
}
